package com.bxait.toymail.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.bxait.com.utils.PageUtils;
import com.bxait.com.utils.Query;
import com.bxait.com.utils.R;
import com.bxait.toymail.ware.feign.MemberFeignService;
import com.bxait.toymail.ware.vo.FareVo;
import com.bxait.toymail.ware.vo.MemberAddressVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bxait.toymail.ware.dao.WareInfoDao;
import com.bxait.toymail.ware.entity.WareInfoEntity;
import com.bxait.toymail.ware.service.WareInfoService;

import javax.annotation.Resource;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {
    @Resource
    MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();

        // 获取请求参数
         String key = (String) params.get("key");
         if(!StringUtils.isEmpty(key)){
             wrapper.and(w->{
                w.eq("id",key).or().like("name",key);
             });
         }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /*
     * @param addrId
     * @==============History===============<br/>
     * @Description //   获取用户收获地址信息
     * @Date 2023/5/13 2023/5/13 
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return com.bxait.toymail.ware.vo.FareVo
     */
    @Override
    public FareVo getFare(Long addrId) {
        FareVo fareVo = new FareVo();
        R r = memberFeignService.addrInfo(addrId);
        if(r.getCode() == 0){
            MemberAddressVo data = r.getData("memberReceiveAddress",new TypeReference<MemberAddressVo>() {});
            if(data != null){
                fareVo.setAddress(data);
                // 计算运费 TODO:将来可对接物流系统
                String phone = data.getPhone();
                String substring = phone.substring(phone.length() - 1, phone.length());
                BigDecimal bigDecimal = new BigDecimal(substring);
                fareVo.setFare(bigDecimal);
                return fareVo;
            }
        }
        return null;
    }


}